package com.hlingsoft.kkh.action;


import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.EventsVo;

import com.hlingsoft.kkh.util.Common;
import com.hlingsoft.kkh.util.EventsVoSort;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 首页
 *
 * @author Herbert
 */

@Controller
public class IndexAction extends BaseAction {


    @Autowired
    private UserDao userDao;


    @Autowired
    private EventsDao eventsDao;


    @Autowired
    private ImageDao imageDao;

    @Autowired
    private FocusDao focusDao;


    @Autowired
    private EventCommentsDao eventCommentsDao;


    @Autowired
    private LikesDao likesDao;


    /**
     * 首页接口
     *
     * @param longitude
     * @param latitude
     * @param request
     * @return
     */
    @RequestMapping(value = "/index.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> checkIn(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "longitude") Double longitude,
            @RequestParam(value = "latitude") Double latitude,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
            //返回数组
            List<List<EventsVo>> resultList = new ArrayList<List<EventsVo>>();


            //最新创建
            List<Events> newestEventsList = eventsDao.getNewestEvents(0, 1);
            List<EventsVo> newestList = new ArrayList<EventsVo>();
            for (Events newestEvents : newestEventsList) {
                EventsVo newestEventsVo = new EventsVo();
                //events基本信息
                newestEventsVo.setId(newestEvents.getId());
                newestEventsVo.setName(newestEvents.getName());

                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(newestEvents.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());


                newestEventsVo.setImageList(imageList);
                newestEventsVo.setAppUserId(newestEvents.getAppUserId());
                newestEventsVo.setCreateTime(newestEvents.getCreateTime());
                newestEventsVo.setCreateAddress(newestEvents.getCreateAddress());
                newestEventsVo.setLatitude(newestEvents.getLatitude());
                newestEventsVo.setLongitude(newestEvents.getLongitude());
                newestEventsVo.setUpdateTime(newestEvents.getUpdateTime());
                newestEventsVo.setDescription(newestEvents.getDescription());
                //获取活动赞
                newestEventsVo.setNumber(likesDao.getLikesByEventsIdCount(newestEvents.getId()));

                newestEventsVo.setType("最新创建");                       //events类型

                newestEventsVo.setAppUser(userDao.getUserById(newestEvents.getAppUserId()));
                newestList.add(newestEventsVo);
            }
            resultList.add(newestList);


            //火热互动
            List<EventsVo> hotEventsVoList = new ArrayList<EventsVo>();
            //获取12小时内跟图最多的活动
            List<Events> hotEventsList = eventsDao.getHotEvents(0, 1);
            if (hotEventsList.size() == 0) {
                hotEventsList = eventsDao.getHotEventsDefault(0, 1);
            }

            for (Events hotEvents : hotEventsList) {
                EventsVo hotEventsVo = new EventsVo();

                hotEventsVo.setId(hotEvents.getId());
                hotEventsVo.setName(hotEvents.getName());


                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(hotEvents.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());

                hotEventsVo.setImageList(imageList);

                hotEventsVo.setAppUserId(hotEvents.getAppUserId());
                hotEventsVo.setCreateTime(hotEvents.getCreateTime());
                hotEventsVo.setCreateAddress(hotEvents.getCreateAddress());
                hotEventsVo.setLatitude(hotEvents.getLatitude());
                hotEventsVo.setLongitude(hotEvents.getLongitude());
                hotEventsVo.setUpdateTime(hotEvents.getUpdateTime());
                hotEventsVo.setDescription(hotEvents.getDescription());
                //获取活动跟
                hotEventsVo.setNumber(eventCommentsDao.getCommentsListByEventIdCount(hotEventsVo.getId()) - 1);

                hotEventsVo.setType("火热互动");

                hotEventsVo.setAppUser(userDao.getUserById(hotEvents.getAppUserId()));
                hotEventsVoList.add(hotEventsVo);
            }
            resultList.add(hotEventsVoList);


            //最新发布
            List<EventsVo> newestCommentList = new ArrayList<EventsVo>();
            List<EventComments> newestEventCommentsList = eventCommentsDao.getNewestEventComments(0, 1);
            for (EventComments newestEventComments : newestEventCommentsList) {
                Events events = eventsDao.getEventsById(newestEventComments.getEventId());

                EventsVo newestCommentEventsVo = new EventsVo();
                //events基本信息
                newestCommentEventsVo.setId(events.getId());
                newestCommentEventsVo.setName(events.getName());

                //活动图片定义为评论的图片
                List<Image> imageList1 = imageDao.getImageByEventCommentsId(newestEventComments.getId());
                newestCommentEventsVo.setImageList(imageList1);

                newestCommentEventsVo.setAppUserId(newestEventComments.getAppUserId());  //用户信息用评论人的信息
                newestCommentEventsVo.setCreateTime(newestEventComments.getCreateTime());
                newestCommentEventsVo.setCreateAddress(events.getCreateAddress());
                newestCommentEventsVo.setLatitude(events.getLatitude());
                newestCommentEventsVo.setLongitude(events.getLongitude());
                newestCommentEventsVo.setUpdateTime(newestEventComments.getUpdateTime());
                //最新发布赞
                newestCommentEventsVo.setNumber(likesDao.getLikesByEventsIdCount(newestCommentEventsVo.getId()));

                newestCommentEventsVo.setDescription(newestEventComments.getContent()); //说两句用评论人的评论
                newestCommentEventsVo.setType("最新发布");                       //events类型
                newestCommentEventsVo.setAppUser(userDao.getUserById(newestEventComments.getAppUserId()));
                newestCommentList.add(newestCommentEventsVo);
            }
            resultList.add(newestCommentList);

//            //点赞最多
//            List<Events> mostLikesEventsList = eventsDao.getMostLikes(0,1);
//            List<EventsVo> mostLikeList = new ArrayList<EventsVo>();
//            for (Events mostLikeEvents : mostLikesEventsList) {
//                EventsVo mostLikeEventsVo = new EventsVo();
//                //events基本信息
//                mostLikeEventsVo.setId(mostLikeEvents.getId());
//                mostLikeEventsVo.setName(mostLikeEvents.getName());
//
//                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(mostLikeEvents.getId());
//                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
//
//                mostLikeEventsVo.setImageList(imageList);
//
//                mostLikeEventsVo.setAppUserId(mostLikeEvents.getAppUserId());
//                mostLikeEventsVo.setCreateTime(mostLikeEvents.getCreateTime());
//                mostLikeEventsVo.setCreateAddress(mostLikeEvents.getCreateAddress());
//                mostLikeEventsVo.setLatitude(mostLikeEvents.getLatitude());
//                mostLikeEventsVo.setLongitude(mostLikeEvents.getLongitude());
//                mostLikeEventsVo.setUpdateTime(mostLikeEvents.getUpdateTime());
//                mostLikeEventsVo.setDescription(mostLikeEvents.getDescription());
//
//                mostLikeEventsVo.setNumber(mostLikeEvents.getNumber());    //点赞数
//                mostLikeEventsVo.setType("点赞最多");                       //events类型
//
//                mostLikeEventsVo.setAppUser(userDao.getUserById(mostLikeEvents.getAppUserId()));
//                mostLikeList.add(mostLikeEventsVo);
//            }
//            resultList.add(mostLikeList);


            //分享最多
            List<Events> mostShareEventsList = eventsDao.getMostShare(0, 1);
            if (mostShareEventsList.size()==0){
                mostShareEventsList=eventsDao.getMostShareDefault(0,1);
            }
            List<EventsVo> mostShareList = new ArrayList<EventsVo>();
            for (Events mostShareEvents : mostShareEventsList) {
                EventsVo mostShareEventsVo = new EventsVo();
                //events基本信息
                mostShareEventsVo.setId(mostShareEvents.getId());
                mostShareEventsVo.setName(mostShareEvents.getName());


                //图片
                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(mostShareEvents.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());

                mostShareEventsVo.setImageList(imageList);
                mostShareEventsVo.setAppUserId(mostShareEvents.getAppUserId());
                mostShareEventsVo.setCreateTime(mostShareEvents.getCreateTime());
                mostShareEventsVo.setCreateAddress(mostShareEvents.getCreateAddress());
                mostShareEventsVo.setLatitude(mostShareEvents.getLatitude());
                mostShareEventsVo.setLongitude(mostShareEvents.getLongitude());
                mostShareEventsVo.setUpdateTime(mostShareEvents.getUpdateTime());
                mostShareEventsVo.setDescription(mostShareEvents.getDescription());
                //events类型
                mostShareEventsVo.setNumber(mostShareEvents.getNumber());    //分享数
                mostShareEventsVo.setType("分享最多");

                mostShareEventsVo.setAppUser(userDao.getUserById(mostShareEvents.getAppUserId()));
                mostShareList.add(mostShareEventsVo);
            }
            resultList.add(mostShareList);

            //评论最多
            List<Events> mostEventsCommentsEventsList = eventsDao.getMostEventComments(0, 1);
            List<EventsVo> mostCommentsList = new ArrayList<EventsVo>();
            for (Events mostEventsCommentEvents : mostEventsCommentsEventsList) {
                EventsVo mostEventsCommentEventsVo = new EventsVo();
                //events基本信息
                mostEventsCommentEventsVo.setId(mostEventsCommentEvents.getId());
                mostEventsCommentEventsVo.setName(mostEventsCommentEvents.getName());


                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(mostEventsCommentEvents.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());

                mostEventsCommentEventsVo.setImageList(imageList);
                mostEventsCommentEventsVo.setAppUserId(mostEventsCommentEvents.getAppUserId());
                mostEventsCommentEventsVo.setCreateTime(mostEventsCommentEvents.getCreateTime());
                mostEventsCommentEventsVo.setCreateAddress(mostEventsCommentEvents.getCreateAddress());
                mostEventsCommentEventsVo.setLatitude(mostEventsCommentEvents.getLatitude());
                mostEventsCommentEventsVo.setLongitude(mostEventsCommentEvents.getLongitude());
                mostEventsCommentEventsVo.setUpdateTime(mostEventsCommentEvents.getUpdateTime());
                mostEventsCommentEventsVo.setDescription(mostEventsCommentEvents.getDescription());
                //events类型
                mostEventsCommentEventsVo.setNumber(mostEventsCommentEvents.getNumber());    //评论数
                mostEventsCommentEventsVo.setType("评论最多");

                mostEventsCommentEventsVo.setAppUser(userDao.getUserById(mostEventsCommentEvents.getAppUserId()));
                mostCommentsList.add(mostEventsCommentEventsVo);
            }
            resultList.add(mostCommentsList);

            resultMap.put("mostList", resultList);


            //热门回顾
//            List<Events> hotEventsList = eventsDao.getMostEventComments();
//            List<EventsVo> hotEventsVoList = new ArrayList<EventsVo>();
//            for (Events hotEvents : hotEventsList) {
//                EventsVo hotEventsVo = new EventsVo();
//                //events基本信息
//                hotEventsVo.setId(hotEvents.getId());
//                hotEventsVo.setName(hotEvents.getName());
////                hotEventsVo.setImages(hotEvents.getImages());
//                List<Image> imageList = imageDao.getImageByEventsId(hotEvents.getId());
//                hotEventsVo.setImageList(imageList);
//
//                hotEventsVo.setAppUserId(hotEvents.getAppUserId());
//                hotEventsVo.setCreateTime(hotEvents.getCreateTime());
//                hotEventsVo.setCreateAddress(hotEvents.getCreateAddress());
//                hotEventsVo.setLatitude(hotEvents.getLatitude());
//                hotEventsVo.setLongitude(hotEvents.getLongitude());
//                hotEventsVo.setUpdateTime(hotEvents.getUpdateTime());
//                hotEventsVo.setDescription(hotEvents.getDescription());
//                //events类型
//                hotEventsVo.setNumber(hotEvents.getNumber());    //评论数
//                hotEventsVo.setType("热门回顾");
//
//                hotEventsVo.setAppUser(userDao.getUserById(hotEvents.getAppUserId()));
//                hotEventsVoList.add(hotEventsVo);
//            }


            //好友动态
            //获取好友
            List<EventsVo> friendsEventsVoList = new ArrayList<EventsVo>();
            List<Focus> focusList = focusDao.getAttentions2ById(userId); //获取5位好友
            for (Focus focus : focusList) {
                EventsVo friendsEventsVo = new EventsVo();
                AppUser user = userDao.getUserById(focus.getAttentionsId());
                //获取好友信息
                friendsEventsVo.setAppUser(user);

                Events friendsEvents = eventsDao.getNewestEventsByUserId(user.getId());
                if (friendsEvents != null) {
                    friendsEventsVo.setId(friendsEvents.getId());
                    friendsEventsVo.setName(friendsEvents.getName());

                    EventComments firstEventComments = eventCommentsDao.getFirstEventComments(friendsEvents.getId());
                    List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());


                    friendsEventsVo.setImageList(imageList);
                    friendsEventsVo.setAppUserId(friendsEvents.getAppUserId());
                    friendsEventsVo.setCreateTime(friendsEvents.getCreateTime());
                    friendsEventsVo.setCreateAddress(friendsEvents.getCreateAddress());
                    friendsEventsVo.setLatitude(friendsEvents.getLatitude());
                    friendsEventsVo.setLongitude(friendsEvents.getLongitude());
                    friendsEventsVo.setUpdateTime(friendsEvents.getUpdateTime());
                    friendsEventsVo.setDescription(friendsEvents.getDescription());

                    //events类型
                    friendsEventsVo.setNumber(likesDao.getLikesByEventsIdCount(friendsEventsVo.getId()));
                    friendsEventsVo.setType("好友动态");
                    friendsEventsVoList.add(friendsEventsVo);

                    EventsVoSort sort = new EventsVoSort();
                    Collections.sort(friendsEventsVoList,sort);
                }
            }
            friendsEventsVoList=Common.fenYe2(friendsEventsVoList,0,5);
            resultMap.put("friendsEventsVoList", friendsEventsVoList);

            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }





}


